package cn.cjlmonster.demo02;

import android.util.Log;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.cjlmonster.demo02.adapter.MainAdapter;
import cn.cjlmonster.demo02.api.Api;
import cn.cjlmonster.demo02.api.BaseResult;
import cn.cjlmonster.demo02.api.callback.LGArrayCallback;
import cn.cjlmonster.demo02.api.result.SnapshotEntity;
import cn.cjlmonster.demo02.base.BaseAppCompatActivity;
import cn.cjlmonster.demo02.decoration.BottomLineDecoration;
import cn.cjlmonster.demo02.ui.AAChartActivity;
import cn.cjlmonster.demo02.ui.CalendarTestActivity;
import cn.cjlmonster.demo02.ui.ChartActivity;
import cn.cjlmonster.demo02.ui.GalleryActivity;
import cn.cjlmonster.demo02.ui.ImagePickerActivity;
import cn.cjlmonster.demo02.ui.LanguageSettingsActivity;
import cn.cjlmonster.demo02.ui.LottieAnimActivity;
import cn.cjlmonster.demo02.ui.RecyclerActivity;
import cn.cjlmonster.demo02.ui.RingGradientActivity;
import cn.cjlmonster.demo02.ui.RuleViewActivity;
import cn.cjlmonster.demo02.ui.ScanActivity;
import cn.cjlmonster.demo02.ui.TestActivity;
import cn.cjlmonster.demo02.ui.V2FragmentActivity;
import cn.cjlmonster.demo02.ui.ViewPager2Activity;
import cn.cjlmonster.demo02.ui.ViewPagerRVActivity;
import cn.cjlmonster.demo02.virtual.MyObserver;

/**
 * This project is Demo02
 * Created by cjl on 2021/11/7 5:31 下午.
 */

public class MainActivity extends BaseAppCompatActivity implements MainAdapter.OnItemClickedListener {

    private static final String TAG = "测试日志";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected boolean canBack() {
        return false;
    }

    @Override
    protected void initView() {
        RecyclerView recyclerView = findViewById(R.id.recyclerView);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(layoutManager);

        String[] mData = {
                getString(R.string.gallery_demo),
                getString(R.string.layout_manager_demo),
                getString(R.string.scan_demo),
                getString(R.string.chart_demo),
                "AAChartCore Demo",
                "Api Demo",
                "ViewPager2 Demo",
                "V2Fragment Demo",
                "Custom Rule View",
                "Ring Gradient View",
                "Test SparseArray",
                "测试",
                "日历控件测试",
                "Lottie Animation",
                "RecyclerView测试",
                "ImagePicker测试"
        };

        MainAdapter adapter = new MainAdapter(this, mData, this);

        recyclerView.setAdapter(adapter);

        recyclerView.addItemDecoration(new BottomLineDecoration(this));

        MyObserver observer = new MyObserver();
        getLifecycle().addObserver(observer);
    }

    @Override
    public void onItemClicked(int position, String item) {
        switch (position) {
            case 0:
                startUI(GalleryActivity.class, item);
                break;
            case 1:
                startUI(ViewPagerRVActivity.class, item);
                break;
            case 2:
                startUI(ScanActivity.class, item);
                break;
            case 3:
                startUI(ChartActivity.class, item);
                break;
            case 4:
                startUI(AAChartActivity.class, item);
                break;
            case 5:
                request();
                break;
            case 6:
                startUI(ViewPager2Activity.class, item);
                break;
            case 7:
                startUI(V2FragmentActivity.class, item);
                break;
            case 8:
                startUI(RuleViewActivity.class, item);
                break;
            case 9:
                startUI(RingGradientActivity.class, item);
                break;
            case 10:
                testSparseArray();
                break;
            case 11:
                startUI(TestActivity.class, null);
                break;
            case 12:
                startUI(CalendarTestActivity.class, null);
                break;
            case 13:
                startUI(LottieAnimActivity.class, null);
                break;
            case 14:
                startUI(RecyclerActivity.class, null);
                break;
            case 15:
                startUI(ImagePickerActivity.class, null);
                break;
            default:
                break;
        }
    }

    private void request() {
        Api.getSnapshotOfFHS(new LGArrayCallback<SnapshotEntity>(SnapshotEntity.class) {
            @Override
            public void onSuccess(BaseResult<List<SnapshotEntity>> result) {
                Log.i(TAG, "onSuccessArray: status = " + result.getStatus());
                Log.i(TAG, "onSuccessArray: msg = " + result.getMsg());
                for (SnapshotEntity e : result.getData()) {
                    Log.i(TAG, "onSuccessArray: " + e);
                }
            }

            @Override
            public void onError(int errorCode, Throwable e) {
                Log.e(TAG, "onFailed: " + errorCode, e);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.action_language) {
            startUI(LanguageSettingsActivity.class, getString(R.string.menu_language_setting));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void testSparseArray() {
        SparseArray<String> modes = new SparseArray<>();
        modes.append(13, "热水");
        modes.append(1, "制热");
        modes.append(10, "制冷");
        modes.append(3, "制热+热水");
        modes.append(5, "制冷+热水");
        Log.i(TAG, "testSparseArray: 13 = " + modes.indexOfKey(13));
        Log.i(TAG, "testSparseArray: 1 = " + modes.indexOfKey(1));
        Log.i(TAG, "testSparseArray: 10 = " + modes.indexOfKey(10));
        Log.i(TAG, "testSparseArray: 3 = " + modes.indexOfKey(3));
        Log.i(TAG, "testSparseArray: 5 = " + modes.indexOfKey(5));
        for (int i = 0; i < modes.size(); i++) {
            int key = modes.keyAt(i);
            String value = modes.valueAt(i);
            Log.i(TAG, "testSparseArray: key = " + key + ", value = " + value);
        }

    }
}
